package com.hwadee.property.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.hwadee.property.pojo.ChargeDetail;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface ChargeDetailMapper extends BaseMapper<ChargeDetail> {

    // 计算总记录数
    @Select("<script>" +
            "SELECT COUNT(*) FROM charge_detail" +
            "<where>" +
            "   <if test='ownerId != null'> AND owner_id = #{ownerId} </if>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if>" +
            "   <if test='houseId != null'> AND house_id = #{houseId} </if>" +
            "   <if test='chargeId != null'> AND charge_id = #{chargeId} </if>" +
            "   <if test='propertyStaffId != null'> AND property_staff_id = #{propertyStaffId} </if>" +
            "   <if test='feeMonth != null'> AND fee_month = #{feeMonth} </if>" +
            "   <if test='paymentStatus != null'> AND payment_status = #{paymentStatus} </if>" +
            "</where>" +
            "</script>")
    int countChargeDetails(@Param("ownerId") Long ownerId,
                           @Param("communityId") Long communityId,
                           @Param("houseId") Long houseId,
                           @Param("chargeId") Long chargeId,
                           @Param("propertyStaffId") Long propertyStaffId,
                           @Param("feeMonth") Integer feeMonth,
                           @Param("paymentStatus") int paymentStatus);

    // 分页查询
    @Select("<script>" +
            "SELECT * FROM charge_detail " +
            "<where>" +
            "   <if test='ownerId != null'> AND owner_id = #{ownerId} </if>" +
            "   <if test='communityId != null'> AND community_id = #{communityId} </if>" +
            "   <if test='houseId != null'> AND house_id = #{houseId} </if>" +
            "   <if test='chargeId != null'> AND charge_id = #{chargeId} </if>" +
            "   <if test='propertyStaffId != null'> AND property_staff_id = #{propertyStaffId} </if>" +
            "   <if test='feeMonth != null'> AND fee_month = #{feeMonth} </if>" +
            "   <if test='paymentStatus != null'> AND payment_status = #{paymentStatus} </if>" +
            "</where> " +
            "ORDER BY id ASC " +
            "LIMIT #{size} OFFSET #{offset}" +
            "</script>")
    List<ChargeDetail> getChargeDetails(@Param("ownerId") Long ownerId,
                                        @Param("communityId") Long communityId,
                                        @Param("houseId") Long houseId,
                                        @Param("chargeId") Long chargeId,
                                        @Param("propertyStaffId") Long propertyStaffId,
                                        @Param("feeMonth") Integer feeMonth,
                                        @Param("paymentStatus") int paymentStatus,
                                        @Param("size") int size,
                                        @Param("offset") int offset);

    /**
     * 根据交易号查询收费详情
     * @param tradeNo 交易号
     * @return 收费详情对象
     */
    @Select("SELECT * FROM charge_detail WHERE trace_no = #{tradeNo}")
    ChargeDetail getByTradeNo(@Param("tradeNo") String tradeNo);
}
